package com.kuaimacode.kframework.api.task;

import com.kuaimacode.kframework.api.service.UserCouponService;
import com.kuaimacode.kframework.api.service.UserMasterService;
import com.kuaimacode.kframework.api.service.user.OrderDispatchService;
import com.kuaimacode.kframework.api.service.user.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务
 */
@Component
public class SchedulerTask {
    private int count=0;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserCouponService couponService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDispatchService orderDispatchService;
    @Autowired
    private UserMasterService masterService;

    //优惠券作废
    @Scheduled(cron="1 0 0 * * ?")
    private void couponValid(){
        int updateCount = couponService.updateCouponStatus();
        logger.info("优惠券到期定时任务执行结束,共更新{}条数据。。",updateCount);

    }

    //订单支付超时取消
    @Scheduled(fixedRate = 59000) //每59秒执行一次
    public void reportCurrentTime() {
        int updateCount = orderService.updateOrderStatus();
        logger.info("订单支付超时定时任务执行结束,共更新{}条数据。。",updateCount);

        //订单待接单超时取消
        //int updateCount1 = orderService.cancleByUnAccept();
        //logger.info("订单待接单超时取消定时任务执行结束,共更新{}条数据。。",updateCount1);

        //订单池有新订单推送
        int updateCount2 = orderService.newOrderPush();
        logger.info("订单池有新订单推送定时任务执行结束,共更新{}条数据。。",updateCount2);
    }


    //订单调度-自动派单
    @Scheduled(fixedRate =59000) //每59秒执行一次
    public void autoDispatch() {
        orderDispatchService.autoDispatch();
        logger.info("订单调度-自动派单定时任务执行结束");
    }

    //订单调度-派单订单发送通知
    @Scheduled(fixedRate =59000) //每59秒执行一次
    public void dispatchPush() {

        //推送1次，电话两次后  改派或者超时取消。
        int updateCount2 = orderService.changeDispatchOrCancle();
        logger.info("订单待接单超时,取消订单 定时任务执行结束,共更新{}条数据",updateCount2);


        //orderDispatchService.autoDispatch();
        int updateCount = orderDispatchService.dispatchPush();
        logger.info("订单调度派单发送通知 定时任务执行结束,共更新{}条数据",updateCount);

    }

    //订单调度-派单订单发送通知
    @Scheduled(fixedRate =30000) //每120秒执行一次
    public void dispatchCall() {

        int updateCount1 = orderDispatchService.dispatchCall();
        logger.info("订单调度派单拨打电话 定时任务执行结束,共更新{}条数据",updateCount1);
    }

    //优惠券作废
    @Scheduled(cron="5 0 0 * * ?")
    private void masterOnlineTimeUpdate(){
        int updateCount = masterService.updateMasterOpLog();
        logger.info("在线日志定时任务执行结束,共更新{}条数据。。",updateCount);

    }
}
